/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package VoiceIdent;

import java.util.ArrayList;

/**
 *
 * @author JArik
 */
public class Centroid extends MfccVector{

    private VoiceData sector;
    private Centroid leftCentroid, rightCentroid;
    private Centroid parentCentroid=null;
    
    public Centroid(VoiceData sect)
    {
        sector = sect;
    }
    
    public Centroid(Centroid parent)
    {
        parentCentroid = parent;
        sector = parent.getSector();
    }
    public boolean isParent()
    {
        return leftCentroid != null && rightCentroid != null;
    }
    
    public VoiceData getSector()
    {
        return sector;
    }
    public void setSector(VoiceData sect)
    {
        sector = sect;
    }
    public Centroid getRightCentroid()
    {
        return rightCentroid;
    }
    public Centroid getLeftCentroid()
    {
        return leftCentroid;
    }
    public void setLeftCentroid(Centroid lF)
    {
        leftCentroid = lF;
    }
    public void setRightCentroid(Centroid rF)
    {
        rightCentroid = rF;
    }

    public void recalcSector() throws Exception
    {
         ArrayList<MfccVector> data = sector.getData();
         VoiceData newSector = new VoiceData();
         // for every mfcc vector in this sector
         for(int i=0; i<data.size(); i++)
         {
             double distance=-1;
             if(data.get(i).getAttachedCentroid() != null &&
                !data.get(i).getAttachedCentroid().isParent() &&
                data.get(i).getDistanceToCentroid() <=
                                                  (distance = data.get(i).getDistance(this)))
                 continue;
             newSector.add(data.get(i));
             data.get(i).attachToCentroid(this, distance);

         }
         sector = newSector;
         //recalcPosition();
    }
    public void recalcPosition()
    {
        ArrayList<MfccVector> data = sector.getData();
        for(int i=0; i<data.size(); i++)
         {
               // for every element in  this vector
              for(int y=0; y < data.get(i).size(); y++)
              {
                    Double dbl = (Double)((data.get(i).get(y))/data.size());
                    if(this.size() > y)
                    {
                        dbl += this.get(y);
                        this.setElementAt(dbl,y);
                    }else
                        this.add(dbl);
              }
        }
    }
}
